using System.Text.Json.Serialization;

namespace MallApi.Common;

/// <summary>
/// API统一返回结果类
/// </summary>
public class ApiResult<T>
{
    /// <summary>
    /// 状态码 0-成功 其他-失败
    /// </summary>
    [JsonPropertyName("code")]
    public int Code { get; set; }

    /// <summary>
    /// 返回信息
    /// </summary>
    [JsonPropertyName("msg")]
    public string Msg { get; set; } = string.Empty;

    /// <summary>
    /// 返回数据
    /// </summary>
    [JsonPropertyName("data")]
    public T? Data { get; set; }

    /// <summary>
    /// 成功
    /// </summary>
    public static ApiResult<T> Success(T? data = default, string msg = "success")
    {
        return new ApiResult<T>
        {
            Code = 0,
            Msg = msg,
            Data = data
        };
    }

    /// <summary>
    /// 失败
    /// </summary>
    public static ApiResult<T> Error(string msg = "error", int code = 500, T? data = default)
    {
        return new ApiResult<T>
        {
            Code = code,
            Msg = msg,
            Data = data
        };
    }
}